package com.yeyks.service.impl;

import com.yeyks.dal.dao.CouponInfoMapper;
import com.yeyks.dal.domain.CouponInfo;
import com.yeyks.model.CouponConst;
import com.yeyks.service.CouponInfoService;
import com.yeyks.dal.dao.CouponInfoMapper;
import com.yeyks.dal.domain.CouponInfo;
import com.yeyks.service.CouponInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author fuchangshuai
 * @date 2019/2/28 1:23 PM
 */
@Service
@Slf4j
public class CouponInfoServiceImpl implements CouponInfoService {

    @Autowired
    private CouponInfoMapper couponInfoMapper;

    /**
     * 通过券id和用户id查询有效的券
     * @param id
     * @param used 是否被使用
     * @param valid 是否有效
     * @param couponId 券id
     * @return
     */
    @Override
    public List<Map> selectByUser(Integer id, Boolean used, Boolean valid, Integer couponId) {
        return couponInfoMapper.selectByUser(id, used, valid, couponId);
    }

    /**
     * 获取充值10元得到的50元的券
     *
     * @param payAmount
     * @return
     */
    @Override
    public CouponInfo selectCouponByPayAmount(Integer payAmount) {
        return couponInfoMapper.selectCouponByPayAmount(payAmount);
    }

    /**
     * 通过优惠券id获取优惠券
     *
     * @param couponId
     * @return
     */
    @Override
    public CouponInfo selectByCouponId(Integer couponId) {
        return couponInfoMapper.selectByPrimaryKey(couponId);
    }

    /**
     * 获取需要发放的随机券
     *
     * @param param
     * @return
     */
    @Override
    public List<CouponInfo> randomCoupon(Map<String, Object> param) {
        return couponInfoMapper.randomCoupon(param);
    }

    /**
     * 是否有推广所用的优惠券
     *
     * @return
     */
    @Override
    public Boolean recommendExist() {
        CouponInfo couponInfo = couponInfoMapper.selectRecommendCoupon(CouponConst.recommend);
        if (couponInfo == null) {
            return false;
        }
        if (couponInfo.getRemainCount() > 0) {
            return true;
        } else {
            return false;
        }
    }

}
